package com.plugin.planilha;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class AnaliseDefasadaStackSheet implements ISheet{

	private WritableWorkbook workbook;
	private HashMap<String, HashSet<String>> mapStack;
	private HashMap<String, HashSet<String>> mapArraydeque;
	private static WritableSheet defasadaStackWS;

	
	public AnaliseDefasadaStackSheet(WritableWorkbook w, HashMap<String, HashSet<String>> map_defasada, HashMap<String, HashSet<String>> map_usada) {
		workbook = w;
		this.mapStack = map_defasada;
		this.mapArraydeque = map_usada;
		defasadaStackWS = workbook.createSheet("Defasada Stack e ArrayDeque", 1);
	}

	@Override
	public void addDados() throws RowsExceededException, WriteException {
		Label label;
		jxl.write.Number n;
		int indice = 1;
		double num_variaveis_defasadas=0, num_variaveis_usadas=0;
		
		for (String classe : mapArraydeque.keySet()) {
			if (mapArraydeque.get(classe).size() > 0) {
				Iterator<String> iterator = mapArraydeque.get(classe).iterator();
				while(iterator.hasNext()){
					String var = iterator.next();
					
					label = new Label(0, indice, var);
					defasadaStackWS.addCell(label);
					indice++;
				}
			}
		}
		indice = 1;
		for (String clsse : mapStack.keySet()) {
			if (mapStack.get(clsse).size() > 0) {
				Iterator<String> iterator = mapStack.get(clsse).iterator();
				while(iterator.hasNext()){
					String var = iterator.next();
					
					label = new Label(1, indice, var);
					defasadaStackWS.addCell(label);
					indice++;
				}
			}
		}
		
		for(String classe : mapStack.keySet())
			num_variaveis_defasadas+= mapStack.get(classe).size();
		for(String classe : mapArraydeque.keySet())
			num_variaveis_usadas+= mapArraydeque.get(classe).size();
		
		double round_comparacao=0;
		if (num_variaveis_defasadas != 0) {
			round_comparacao = (double) Math
					.round(((num_variaveis_defasadas * 100)
							/ (num_variaveis_usadas + num_variaveis_defasadas)) * 1000) / 1000;
		}
		
		n = new jxl.write.Number(3, 1, (int) num_variaveis_defasadas);
		defasadaStackWS.addCell(n);
		
		n = new jxl.write.Number(4, 1, (int) num_variaveis_usadas);
		defasadaStackWS.addCell(n);
		
		n = new jxl.write.Number(5, 1, round_comparacao);
		defasadaStackWS.addCell(n);
		
		
	}

	@Override
	public void criaCabecalho(WritableCellFormat cf)
			throws RowsExceededException, WriteException {

		Label label = new Label(0, 0, "Variaveis ArrayDeque", cf);
		defasadaStackWS.addCell(label);
		
		label = new Label(1, 0, "Variaveis Stack", cf);
		defasadaStackWS.addCell(label);
		
		label = new Label(3, 0, "Numero de instancias Stack", cf);
		defasadaStackWS.addCell(label);
		
		label = new Label(4, 0, "Numero instancias de ArrayDeque", cf);
		defasadaStackWS.addCell(label);
		
		label = new Label(5, 0, "Taxa de comparacao", cf);
		defasadaStackWS.addCell(label);
	}


}
